package cn.org.eshow.component.parser.http;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 
 * @author leida
 */

public class Client {

	private static HttpClient client = new DefaultHttpClient();
	private final static String CONTENT_TYPE = "application/x-www-form-urlencoded";
	private final static String ENCODING = "utf-8";

	public static String post(String url, List<BasicNameValuePair> pairs) throws Exception {
		String result = null;
		HttpPost httppost = new HttpPost(url);

		HttpEntity entity;
		UrlEncodedFormEntity initEntity = new UrlEncodedFormEntity(pairs, ENCODING);

		initEntity.setContentType(CONTENT_TYPE);

		httppost.setEntity(initEntity);
		HttpResponse response = client.execute(httppost);

		entity = response.getEntity();

		if (entity != null) {
			InputStream instream = null;
			try {
				instream = entity.getContent();
				result = IOUtils.toString(instream);

			} catch (IOException ex) {

				throw ex;

			} catch (RuntimeException ex) {

				httppost.abort();
				throw ex;

			} finally {

				instream.close();

			}
		}
		return result;
	}

    /**
     * post json格式数据
     * @param url
     * @param pairs json格式
     * @return
     * @throws Exception
     */
    public static String post(String url, String pairs) throws Exception {
        String result = null;
        HttpPost httpPost = new HttpPost(url);

        StringEntity entity = new StringEntity(pairs, HTTP.UTF_8);
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(httpPost);

        HttpEntity httpEntity = response.getEntity();

        if (httpEntity != null) {
            InputStream instream = null;
            try {
                instream = httpEntity.getContent();
                result = IOUtils.toString(instream);

            } catch (IOException ex) {

                throw ex;

            } catch (RuntimeException ex) {

                httpPost.abort();
                throw ex;

            } finally {

                instream.close();

            }
        }
        return result;
    }

	public static String get(String url) throws Exception {
		String result = null;
		HttpGet httpget = new HttpGet(url);

		HttpResponse response = new DefaultHttpClient().execute(httpget);

		HttpEntity entity = response.getEntity();

		if (entity != null) {
			InputStream instream = null;
			try {
				instream = entity.getContent();
				result = IOUtils.toString(instream);

			} catch (IOException ex) {

				throw ex;

			} catch (RuntimeException ex) {

				httpget.abort();
				throw ex;

			} finally {

				instream.close();

			}
		}
		return result;
	}

	public static void main(String[] args) throws Exception {
//		List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
//		pairs.add(new BasicNameValuePair("code", "4/qTB1UgaxrfbQT06bys91SnVkutW1.cje8NF3DkvsbgrKXntQAax0C_aQNeAI"));
//		pairs.add(new BasicNameValuePair("client_id", "1010833784173.apps.googleusercontent.com"));
//		pairs.add(new BasicNameValuePair("client_secret", "u8uRbFvdQPwXLBvfhUmNEGXq"));
//
//		pairs.add(new BasicNameValuePair("redirect_uri", "http://bangqu.com/oauth/callback/google"));
//		pairs.add(new BasicNameValuePair("grant_type", "authorization_code"));
//		String json;
//		try {
//			json = Client.post(
//					"https://accounts.google.com/o/oauth2/token", pairs);
//			System.out.println("json:" + json.toString());
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		String url ="http://api.app.bangqu.com/user/login.json?user.username=zhang20084@126.com&user.password=123";
//        Map<String, String> httpHeaderMap = new HashMap<String, String>();
//        HttpClientManager manager = new HttpClientManager();
//
//        HttpResponseWrapper res = manager.execute(url, "utf-8", "", "", httpHeaderMap);
//        byte[] responseBody = res.getBody();
//
//        String result = new String(responseBody, res.getCharset());

//        JSONObject jsonObject = new JSONObject(result);
//         String status = (String)jsonObject.get("status");
//        System.out.println(status);

	}
}
